/**
 * Copyright 厦门中软海晟信息技术有限公司 版权所有 违者必究 2019
 */
package com.opencvjava.lessons.feature2d;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.highgui.HighGui;
import org.opencv.imgproc.Imgproc;
import org.springframework.stereotype.Service;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Image;

import static com.opencvjava.support.util.CvUtils.mat;
import static org.opencv.core.CvType.CV_8U;
import static org.opencv.imgproc.Imgproc.COLOR_BGR2GRAY;
import static org.opencv.imgproc.Imgproc.cvtColor;

/**
 * @author : sunzb(sunzb@hsit.com.cn)
 * @date: 2019/1/15
 */
@Service
public class L1_HarrisCornerDetector {
    private Mat srcGray;
    private Mat dst;
    private Mat dstNorm;
    private Mat dstNormScaled;
    private JFrame frame;
    private JLabel imgLabel;
    private JLabel cornerLabel;
    private static final int MAX_THRESHOLD = 255;
    private int threshold = 200;

    public void test() {
        srcGray = new Mat();
        dst = new Mat();
        dstNorm = new Mat();
        dstNormScaled = new Mat();
        Mat src = mat("feature2d","building.jpg");
        if (src.empty()) {
            throw new RuntimeException("图片读取失败");
        }
        cvtColor(src, srcGray, COLOR_BGR2GRAY);
        // Create and set up the window.
        frame = new JFrame("Harris corner detector demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // Set up the content pane.
        Image img = HighGui.toBufferedImage(src);
        addComponentsToPane(frame.getContentPane(), img);
        // Use the content pane's default BorderLayout. No need for
        // setLayout(new BorderLayout());
        // Display the window.
        frame.pack();
        frame.setVisible(true);
        update();
    }

    private void addComponentsToPane(Container pane, Image img) {
        if (!(pane.getLayout() instanceof BorderLayout)) {
            pane.add(new JLabel("Container doesn't use BorderLayout!"));
            return;
        }
        JPanel sliderPanel = new JPanel();
        sliderPanel.setLayout(new BoxLayout(sliderPanel, BoxLayout.PAGE_AXIS));
        sliderPanel.add(new JLabel("Threshold: "));
        JSlider slider = new JSlider(0, MAX_THRESHOLD, threshold);
        slider.setMajorTickSpacing(20);
        slider.setMinorTickSpacing(10);
        slider.setPaintTicks(true);
        slider.setPaintLabels(true);
        slider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JSlider source = (JSlider) e.getSource();
                threshold = source.getValue();
                update();
            }
        });
        sliderPanel.add(slider);
        pane.add(sliderPanel, BorderLayout.PAGE_START);
        JPanel imgPanel = new JPanel();
        imgLabel = new JLabel(new ImageIcon(img));
        imgPanel.add(imgLabel);
        Mat blackImg = Mat.zeros(srcGray.size(), CV_8U);
        cornerLabel = new JLabel(new ImageIcon(HighGui.toBufferedImage(blackImg)));
        imgPanel.add(cornerLabel);
        pane.add(imgPanel, BorderLayout.CENTER);
    }
    private void update() {
        dst = Mat.zeros(srcGray.size(), CvType.CV_32F);
        // blockSize -> lambda1 lambda2
        int blockSize = 2;
        // ksize 卷积的窗口大小
        int apertureSize = 3;
        // 0.04 ~ 0.06
        double k = 0.04;
        // detM = lambda1 * lambda2, traceM = lambda1 + lambda2
        // R = detM - k * pow(traceM, 2)
        // R结果要经过 threshold阈值过滤，大于有效
        Imgproc.cornerHarris(srcGray, dst, blockSize, apertureSize, k);
        Core.normalize(dst, dstNorm, 0, 255, Core.NORM_MINMAX);
//        dstNorm.convertTo(dstNormScaled, CV_8U);
        Core.convertScaleAbs(dstNorm, dstNormScaled); // 效果一样
        float[] dstNormData = new float[(int) (dstNorm.total() * dstNorm.channels())];
        dstNorm.get(0, 0, dstNormData);
        for (int i = 0; i < dstNorm.rows(); i++) {
            for (int j = 0; j < dstNorm.cols(); j++) {
                if ((int) dstNormData[i * dstNorm.cols() + j] > threshold) {
                    Imgproc.circle(dstNormScaled, new Point(j, i), 5, new Scalar(0), 2, 8, 0);
                }
            }
        }
        cornerLabel.setIcon(new ImageIcon(HighGui.toBufferedImage(dstNormScaled)));
        frame.repaint();
    }
}
